package domotica.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import domotica.client.EnvironmentThread;
import domotica.client.StateThread;
import domotica.houseowner.HouseOwner;
import domotica.houseowner.Parameter;

/**
 * Classe utilizzata da DomoticaGUI per la visualizzazione delle informazioni 
 * del sistema osservato e per la definizione di un task su un singolo componente.
 */

@SuppressWarnings("serial")
public class ActuatorSensorGUI extends JPanel implements ActionListener, ChangeListener{
	
	private DomoticaGUI domoticaGUI;
	private String actuatorSensorName;
	private HouseOwner houseOwner;
	
	private ArrayList<JSlider> jSliderList;
	
	private JScrollPane slidersScrollPane;
	private JScrollPane tableScrollPane;
	
	private JButton onOffButton;
	private JButton switchTableButton;
	private SlidersJPanel slidersJPanel;
	private JPanel onOffButtonPanel;
	private JPanel buttonAndTablePanel;
	private JTable environmentTable;
	private JTable stateTable;
	
	public ActuatorSensorGUI(DomoticaGUI domoticaGUI, HouseOwner houseOwner){
		this.houseOwner=houseOwner;
		this.domoticaGUI=domoticaGUI;
		this.initGUI();
	}
	
	public void setActuatorSensorParametersSliders(String actuatorSensorName) {
		
		onOffButton.setEnabled(true);
		
		this.actuatorSensorName=actuatorSensorName;
		
		jSliderList = new ArrayList<JSlider>();
		
		slidersJPanel = new SlidersJPanel();
		BoxLayout jPanel2Layout = new BoxLayout(slidersJPanel, javax.swing.BoxLayout.Y_AXIS);
		slidersJPanel.setLayout(jPanel2Layout);
		slidersJPanel.setSize(250,250);
		slidersScrollPane.setViewportView(slidersJPanel);
		
		ArrayList<Parameter> parameterList= new ArrayList<Parameter>(this.houseOwner.getActuatorSensorTaskDesiredParameterList(actuatorSensorName));
		
		JSlider slider;
		JLabel label;
		String parameterName;
		int min;
		int max;
	
		boolean isOn = this.houseOwner.isActuatorSensorOn(actuatorSensorName);
		
		if (isOn){
			onOffButton.setText("SPEGNI");
		} else
			onOffButton.setText("ACCENDI");
		
		for(Parameter parameter: parameterList){
			
			parameterName = parameter.getPropertyName();

			min = parameter.getMin();
			max = parameter.getMax();
			
			label = new JLabel(parameterName);
			label.setMaximumSize(new Dimension(240, 20));
			slidersJPanel.add(label);
			slider = new JSlider(min,max,(max+min)/2);
			jSliderList.add(slider);
			slider.setMajorTickSpacing(10);
			slider.setMinorTickSpacing(5);
			slider.setPaintTicks(true);
			slider.setPaintLabels(true);
			slider.setMaximumSize(new Dimension(550, 80));
			slider.addChangeListener(this);
			slidersJPanel.add(slider);
		}
		this.domoticaGUI.addActuatorSensorCacheElement(actuatorSensorName, slidersJPanel);
		slidersJPanel.setJSliders(this.jSliderList);
	}
		
	public void setActuatorSensorParametersSliders(SlidersJPanel panel, String actuatorSensorName) {
		slidersJPanel=panel;
		slidersScrollPane.setViewportView(slidersJPanel);
		this.actuatorSensorName=actuatorSensorName;
		
		boolean isOn = houseOwner.isActuatorSensorOn(actuatorSensorName);
		jSliderList=panel.getJSliders();
		
		if (isOn){
			onOffButton.setText("SPEGNI");
		} else {
			onOffButton.setText("ACCENDI");
		}
		
	}

	private void initGUI() {
		try {
			{
				GridLayout thisLayout = new GridLayout(1, 1);
				thisLayout.setColumns(1);
				thisLayout.setHgap(5);
				thisLayout.setVgap(5);
				this.setLayout(thisLayout);
				this.setSize(250, 250);
				this.setPreferredSize(new java.awt.Dimension(250, 250));
				{
					slidersScrollPane = new JScrollPane();
					this.add(slidersScrollPane);
					{
						jSliderList = new ArrayList<JSlider>();
						slidersJPanel = new SlidersJPanel();
						BoxLayout jPanel2Layout = new BoxLayout(slidersJPanel, javax.swing.BoxLayout.Y_AXIS);
						slidersJPanel.setLayout(jPanel2Layout);
						slidersJPanel.setSize(250,250);
						slidersScrollPane.setViewportView(slidersJPanel);
					}
				}
				{
					buttonAndTablePanel = new JPanel();
					GridLayout jPanel1Layout = new GridLayout(2, 1);
					jPanel1Layout.setColumns(1);
					jPanel1Layout.setHgap(5);
					jPanel1Layout.setVgap(5);
					jPanel1Layout.setRows(2);
					buttonAndTablePanel.setLayout(jPanel1Layout);
					{
						
					}
					this.add(buttonAndTablePanel);
					{
						onOffButtonPanel = new JPanel();
						buttonAndTablePanel.add(onOffButtonPanel);
						GridBagLayout jPanel3Layout = new GridBagLayout();
						jPanel3Layout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
						jPanel3Layout.rowHeights = new int[] {7, 7, 7, 7};
						jPanel3Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
						jPanel3Layout.columnWidths = new int[] {7, 7, 7, 7};
						onOffButtonPanel.setLayout(jPanel3Layout);
						{
							onOffButton = new JButton();
							onOffButton.setBackground(new Color(103,131,157));
							onOffButtonPanel.add(onOffButton, new GridBagConstraints(1, 1, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							
							onOffButton.addActionListener(this);
							
							onOffButton.setText("ACCENDI");
							
							onOffButton.setEnabled(false);
						}
						{
							switchTableButton = new JButton();
							switchTableButton.setBackground(new Color(103,131,157));
							onOffButtonPanel.add(switchTableButton, new GridBagConstraints(1, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
							switchTableButton.setText("STATO COMPONENTI");
							switchTableButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {

									if (switchTableButton.getText().equals("STATO AMBIENTE")){
										tableScrollPane.setViewportView(environmentTable);
										switchTableButton.setText("STATO COMPONENTI");
									} else {
										tableScrollPane.setViewportView(stateTable);
										switchTableButton.setText("STATO AMBIENTE");
									}
									
								}
							});
						}
					}
					{
						tableScrollPane = new JScrollPane();
						buttonAndTablePanel.add(tableScrollPane);
						{
							
							environmentTable = new JTable();
							
							Thread environmentThread = new Thread(new EnvironmentThread(environmentTable, this.houseOwner));
							environmentThread.start();
							
							stateTable = new JTable();
							
							Thread stateThread = new Thread(new StateThread(stateTable, this.houseOwner));
							stateThread.start();
													
							tableScrollPane.setViewportView(environmentTable);

							

						}
					}
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
	
		if (onOffButton.getText().equals("ACCENDI")){
			
			onOffButton.setText("SPEGNI");
		
			ArrayList<String> parameters = new ArrayList<String>(); 
			
			for (JSlider slider : this.jSliderList){
				parameters.add(String.valueOf(slider.getValue()));
		 	}
			
			this.houseOwner.addActuatorSensorTask(this.actuatorSensorName, parameters);
			
		} else {
			
			onOffButton.setText("ACCENDI");
			
			houseOwner.stopTask(actuatorSensorName);
		}
	}

	public void stateChanged(ChangeEvent e) {
		JSlider source = (JSlider)e.getSource();
		
		if (!source.getValueIsAdjusting()) 
			if (onOffButton.getText().equals("SPEGNI")){

				ArrayList<String> parameters = new ArrayList<String>();
				for (JSlider slider : this.jSliderList){
					parameters.add(String.valueOf(slider.getValue()));
				}
				this.houseOwner.stopTask(this.actuatorSensorName);
				this.houseOwner.addActuatorSensorTask(this.actuatorSensorName, parameters);
			}
	}

}
